A Universal Model for Designing the Entity EJB Layer by Jonathon Leibundguth, Michael Pease, and Andrei Povodyrev Example 1: Universum findByPrimaryKey(UniversumPK pk) throws FinderException, RemoteException; Universum create(UniversumData data, UniversumPK pk) throws RemoteException, CreateException; Collection findBySQLWhere(String where, UniversumPK pk) throws FinderException, RemoteException; Example 2: public static java.util.Collection findByName(String qst, UniversumHome home) throws SQLException, FinderException, RemoteException{ String where = " FIRST_NAME = '" + qst + "'"; return home.findBySQLWhere(where, UserPK.getInstance()); } Example 3: void setBeanAttributes(EntityBeanData ebw) throws RemoteException; EntityBeanData getBeanAttributes() throws RemoteException; Example 4: // First we need to insert a record into the database. // Define the data elements of the record to insert HashMap hm = new HashMap(); hm.put("ID", new Integer(102)); hm.put("FIRST_NAME","JP"); hm.put("LAST_NAME","LEIBUNDGUTH"); hm.put("ZIP_CODE","22182"); hm.put("PHONE","571-633-8645"); UniversumHome home; // Lookup home interface for Universum Bean with appropriate JNDI string // Convert our HashMap of data to the Value object for Universum Bean UniversumData ebd = new UniversumData(hm); // Use the create method on the home interface to insert the record Universum user = home.create(ebd, UserPK.getInstance()); // Next, retrieve the first name of the user we just created. UniversumData data = user.getBeanAttributes()); String name = (String) data.get("FIRST_NAME"); // Finally, change the user's phone number and update the record HashMap hm = new HashMap(); hm.put("PHONE","703-770-3000"); user.setBeanAttributes(new UniversumData(hm)); Listing One package org.article.entitybean; import java.sql.*; import org.article.util.UniversumData; import java.util.*; /* Universal EJB primary key class that defines an interface that each * concrete primary key class must implement. * @version: 1.0 * @author: Andrei Povodyrev */ public interface UniversumPK extends java.io.Serializable{ public int hashCode(); //required by EJB specs public String toString(); //required by EJB specs /* Implement this to determine primary key equality with another of * same type, required by EJB specs * @param obj another UniversumPK * @return true if the primary keys are the same */ public boolean equals(Object obj); public UniversumPK insert(UniversumData beanAttributes, Connection con) throws SQLException; /* Called to delete the row represented by this primary key * @exception SQLException */ public void delete(Connection con) throws SQLException; /* Called to update the row represented by this primary key * @exception SQLException */ public void update(UniversumData beanAttributes, Connection con) throws SQLException; /* Called to select the row represented by this primary key * @return UniversumData * @exception SQLException */ public UniversumData select(Connection con) throws SQLException; /* Called to test for existance of the record represented by * this primary key * @return true if the record exists * @exception SQLException */ public boolean exists(Connection con) throws SQLException; /* Finds records using a SQL where clause * @param where the SQL where clause * @return an enumeration of records * @exception SQLException */ public Collection findBySQLWhere(String where, Connection con) throws SQLException; /* Validates entry data which keys must match those specified by * the concrete primary key class * @return true if entry data valid **/ public boolean validate(UniversumData attributes)throws SQLException; } Listing Two package org.article.util; import java.util.*; /* Sun's recomendation for value objects: * fine-grained, dependent, i.e controlled by another object, * and immutable, i.e. fields are not independently modifiable **/ public class UniversumData implements java.io.Serializable, Cloneable { private HashMap _attributeMap; public UniversumData(HashMap map){ this.setMap(map); } protected void setMap(HashMap map) { _attributeMap = map; } public HashMap getMap(){ return _attributeMap; } public Object get(String key){ return _attributeMap == null?null:_attributeMap.get(key); } public Object clone() throws CloneNotSupportedException{ // implement clone ... } 3